import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store";
import home from './modules/home' //公共部分
import {queryPersonalcInfo,queryHealthIndexBaseData} from '../utils/api.js'
import utils from '@js/utils.js' /* 共用的方法 */
Vue.use(VueRouter);

const router = new VueRouter({
  scrollBehavior(to,from,savedPosition){
    if (savedPosition) {
      return savedPosition
    } else {
      if (from.meta.keepAlive) {
        from.meta.savedPosition = document.body.scrollTop;
      }
      return { x: 0, y: to.meta.savedPosition ||0}
    }
  },
  // mode: 'history',
  routes: [].concat(home)
});



router.beforeEach(function (to, from, next) {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  store.commit('setLoadingStatus',true);

  var padId = utils.apppagePadId();
  if(process.env.NODE_ENV == 'development'){// 开发环境自由设置padId
    padId="631261689169" //225093476949284 225093476953714  631261689169(艳艳)
    utils.setCookie("padId",padId)
  }else{
    utils.setCookie("padId",padId)
  }
  //用户信息
  if(!utils.getSession("userInfo")){
    queryHealthIndexBaseData().then((res)=>{
      //var res={"returnCode":"0","respCode":"0","respMsg":"成功","returnMsg":"成功","data":{"registMonth":"10","userInfo":{"userName":"hhb","LastDays":28,"userPic":"webpage/tools/images/defaultHead.png","userHeight":158,"userWeight":null,"userAge":43,"userSex":"01"},"userIllHis":[],"expireYear":"2019","expireMonth":"11","registYear":"2019"}};
      res.data.padId=padId;
      utils.setSession("userInfo",res.data);
      next();
    }).catch((error)=>{
      next();
    });
    return false;
  }

  next();
});
router.afterEach(function (to, from) {
  store.commit('setLoadingStatus',false);
})
export default router;
